package com.sbtr.controller;

import com.sbtr.business.publishtask.service.IPublishPredictTaskService;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.open.publishtask.dto.PredictListDto;
import com.sbtr.open.publishtask.dto.PublishPredictTaskDto;
import com.sbtr.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping("/predicttask/predict-task")
//@Api(tags = "开放能力预测管理（废弃，暂不使用）")
@ApiIgnore
public class PublishPredictTaskController {

	@Autowired
	private IPublishPredictTaskService publishPredictTaskService;

	@Autowired
	private MinioUtil minioUtil;

	@ApiOperation(value = "根据id查找", notes = "根据id查找", httpMethod = "GET")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Response<PublishPredictTaskDto> findById(@PathVariable("id") Long id) {
		return Response.success(publishPredictTaskService.findById(id));
	}

	@ApiOperation(value = "保存预测任务", notes = "保存预测任务", httpMethod = "POST")
	@RequestMapping(value = "", method = RequestMethod.POST)
	public Response<PublishPredictTaskDto> save(@RequestBody PublishPredictTaskDto dto) {
		return Response.success(publishPredictTaskService.save(dto));
	}

	@ApiOperation(value = "获取预测列表", notes = "获取预测列表", httpMethod = "POST")
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public Response<PageDto<PublishPredictTaskDto>> search(@RequestBody PredictListDto dto) {
		return Response.success(publishPredictTaskService.search(dto));
	}


	@ApiOperation(value = "获取历史预测列表", notes = "获取历史预测列表", httpMethod = "POST")
	@RequestMapping(value = "/searchList", method = RequestMethod.POST)
	public Response<PageDto<PublishPredictTaskDto>> searchList(@RequestBody PredictListDto dto) {
		return Response.success(publishPredictTaskService.searchList(dto));
	}




	@ApiOperation(value = "根据id查找获取文件", notes = "根据id查找获取文件", httpMethod = "GET")
	@RequestMapping(value = "/{id}/getResultUrl", method = RequestMethod.GET)
	@ResponseBody
	public void getResultUrl(@PathVariable("id") Long id,
										 HttpServletResponse resp
										 ) {

		OutputStream out;
		// 读取本地文件的输入流

		InputStream in;
		File file;
		try {

			PublishPredictTaskDto task=publishPredictTaskService.findById(id);

			String objectName=task.getResultFile().substring(task.getResultFile().lastIndexOf("/")+1,task.getResultFile().length());
			file=new File(task.getResultFile());
			in = new FileInputStream(file);
			// 设置响应正文的MIME类型
//			resp.setContentType("Content-Disposition;charset=UTF-8");
			resp.setContentType("application/octet-stream");

			resp.setHeader("Content-Disposition", "attachment;" + " filename=" + objectName);
			// 把本地文件发送给客户端
			out = resp.getOutputStream();
			int byteRead = 0;
			byte[] buffer = new byte[512];
			while ((byteRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, byteRead);
			}
			in.close();
			out.flush();
			out.close();
		}catch (Exception e){
			e.printStackTrace();
		}


//		return Response.success(publishPredictTaskService.getResultUrl(id));
	}

	@ApiOperation(value = "根据publishId获取对应的预测数据", notes = "根据publishId获取对应的预测数据", httpMethod = "GET")
	@RequestMapping(value = "/findByPublishId/{publishId}", method = RequestMethod.GET)
	public Response<List<PublishPredictTaskDto>> findByPublishId(@PathVariable("publishId") Long publishId) {
		return Response.success(publishPredictTaskService.findByPublishId(publishId));
	}

	@ApiOperation(value = "根据id查找获取当前预测的百分比")
	@RequestMapping(value = "/{id}/getPredictPercent", method = RequestMethod.GET)
	public Response<Double> getPredictPercent(@PathVariable("id") Long id) {
		return Response.success(publishPredictTaskService.getPredictPercent(id));
	}
}
